//
//  OrderViewController.m
//  ATMCash
//
//  Created by zhengchengfeng on 2024/9/6.
//

#import "OrderViewController.h"

#import "LXSegmentTitleView.h"
#import "LXScrollContentView.h"

#import "OrderListViewController.h"

@interface OrderViewController ()<LXSegmentTitleViewDelegate,LXScrollContentViewDelegate>

@property (nonatomic) UIImageView *imgV;

@property (nonatomic) NSMutableArray *dataSource;

@property (nonatomic) LXSegmentTitleView *titleView;

@property (nonatomic) LXScrollContentView *contentView;


@end

@implementation OrderViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGB(4, 44, 8);
    
    UIImage *imgIcon = [UIImage imageNamed:@"icon_orderBgv"];
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, DimensionBaseIphone6(imgIcon.size.height))];
    [self.view addSubview:imgV];
    imgV.image = imgIcon;
    self.imgV = imgV;
    
    UILabel *lab = [Utility createLabel:[UIFont fontWithName:kFuturaMedium size:16] color:[UIColor colorWithHexString:@"#FFFFFF"]];
    lab.text = @"Order";
    lab.textAlignment = 1;
    [self.view addSubview:lab];
    lab.frame = CGRectMake(0, DimensionBaseIphone6(44), kScreenWidth, DimensionBaseIphone6(44));
    
    self.dataSource = @[@"ALL",@"Applying",@"Repayment",@"Finish"].mutableCopy;
    [self setupUI];
    [self reloadData];
    
}

#pragma mark ------------------------
- (void)setupUI{
    self.titleView = [[LXSegmentTitleView alloc] initWithFrame:CGRectZero];
    self.titleView.itemMinMargin = 15.f;
    self.titleView.delegate = self;
    self.titleView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.titleView];
    self.titleView.titleFont = [UIFont fontWithName:kFuturaMedium size:13];
    self.titleView.titleSelectFont = [UIFont fontWithName:kFuturaMedium size:13];
    self.titleView.titleSelectedColor = [UIColor colorWithHexString:@"#FFFFFF"];
    self.titleView.titleNormalColor = RGB(73, 120, 84);

    self.titleView.btnBackSelectImg = [UIImage imageNamed:@"icon_orderSel"];
    self.titleView.btnBackNormalImg = [UIImage imageNamed:@"icon_orderUnsel"];
    
    self.titleView.alpha = 1;
    
    self.contentView = [[LXScrollContentView alloc] initWithFrame:CGRectZero];
    self.contentView.delegate = self;
    [self.view addSubview:self.contentView];
}

- (void)segmentTitleView:(LXSegmentTitleView *)segmentView selectedIndex:(NSInteger)selectedIndex lastSelectedIndex:(NSInteger)lastSelectedIndex{
    self.contentView.currentIndex = selectedIndex;

}

- (void)contentViewDidScroll:(LXScrollContentView *)contentView fromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(float)progress{
    
}

- (void)contentViewDidEndDecelerating:(LXScrollContentView *)contentView startIndex:(NSInteger)startIndex endIndex:(NSInteger)endIndex{
    self.titleView.selectedIndex = endIndex;
    
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.titleView.frame = CGRectMake(0, self.imgV.bottom + 5, kScreenWidth, 50);
    self.contentView.frame = CGRectMake(0, self.titleView.bottom , kScreenWidth, kScreenHeight - 50 - self.imgV.bottom - 5);
}

- (void)reloadData{
    
    self.titleView.segmentTitles = self.dataSource;
    
    NSMutableArray *vcs = [[NSMutableArray alloc] init];
    for (int i=0; i<self.dataSource.count; i++) {
        OrderListViewController *vc = [[OrderListViewController alloc] init];
        vc.categoryIndex = i;
        [vcs addObject:vc];
    }
    
    [self.contentView reloadViewWithChildVcs:vcs parentVC:self];

    self.titleView.selectedIndex = 0;
    self.contentView.currentIndex = 0;
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
